"""
封装了带进度条的进程池
参数：
    func：多线程的执行函数
    iterator：func需求的参数，如果是多参数的func，
        iterator应该是列表，而且其中的内容为func
        参数组成的元组或列表
    works：启动的线程数，默认None，开启计算机默认线程数

"""

from concurrent import futures
from collections import abc

from tqdm import tqdm

def mult_thread_run(func,iterator,workers=None):
    with futures.ThreadPoolExecutor(max_workers=workers) as pool:
        tasks = []
        for params in iterator:
            if isinstance(params,abc.Sequence):
                tasks.append(pool.submit(func,*params))
            else:
                tasks.append(pool.submit(func,params))
        for task in tqdm(futures.as_completed(tasks),total=len(tasks)):
            pass

if __name__ == "__main__":
    ## 测试
    import time
    import random
    def test1(x,y):
        time.sleep(y/x)
    def test2(x):
        time.sleep(x/10)

    a = list(range(1,11))
    b = list(range(2,12))
    random.shuffle(a)
    random.shuffle(b)
    paramList = zip(a,b)
    start = time.time()
    mult_thread_run(test1,paramList)
    print(time.time()-start)
    start = time.time()
    mult_thread_run(test2,a)
    print(time.time()-start)